using Microsoft.AspNetCore.Mvc;
using Process.Application.Services;
using Process.Domain.DTO.InputDTO;
using Process.Domain.DTO.OutDTO;
using Process.Domain.Entities;
using Process.Result;
using System.Text.Json;
using System.Linq;
using Newtonsoft.Json;

namespace Process.Api.Controllers
{
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class ProcessModelController : ControllerBase
    {
        private readonly IProcessAppService<ProcessModel> _processModelAppService;
        public ProcessModelController(IProcessAppService<ProcessModel> processModelAppService) 
        {
            _processModelAppService = processModelAppService;
        }

        /// <summary>
        /// 添加工序
        /// </summary>
        /// <param name="processDTO">工序数据传输对象</param>
        /// <returns>API结果</returns>
        [HttpPost]
        public ApiResult<int> AddProcessModel( DTO.InputDTO.AddProcessDTO processDTO)
        {
            // 直接调用业务层方法，业务逻辑由业务层处理
            return _processModelAppService.AddProcess(processDTO);
        }

        /// <summary>
        /// 查询工序
        /// </summary>
        /// <param name="input">参数</param>
        /// <returns>封装响应类</returns>
        [HttpGet]
        public Apipaging<PagingProcessDTO> ShowProcess([FromQuery]PagingProcessInput input)
        {
            return _processModelAppService.PagingProcess(input);
        }
        /// <summary>
        /// 删除工序
        /// </summary>
        /// <param name="input">参数</param>
        /// <returns>封装响应类</returns>
        [HttpDelete]
        public ApiResult<int> DeleteProcessModel(long Id)
        {
            return _processModelAppService.DeleteProcess(Id);
        }

        /// <summary>
        /// 修改工序
        /// </summary>
        /// <param name="input">参数</param>
        /// <returns>封装响应类</returns>
        [HttpPost]
        public ApiResult<int> UpdateProcessModel( UpdateProcessInput input)
        {
            return _processModelAppService.UpdateProcess(input);
        }

        [HttpGet]
        public Apipaging<ShowprocessDTO> VueShowProcess()
        {
            return _processModelAppService.ShowProcess();
        }

        /// <summary>
        /// 工艺流程排序
        /// </summary>
        /// <param name="workflow">工艺流程数据</param>
        /// <returns>排序后的工艺流程</returns>
        [HttpPost]
        public List<StepNode> paixu([FromBody] WorkflowSortInput workflow)
        {
            try
            {
                return _processModelAppService.SortProcessFlow22(workflow);
            }
            catch (Exception ex)
            {
                return new List<StepNode>();
            }
        }

        /// <summary>
        /// 工艺流程排序 (字符串输入方式)
        /// </summary>
        /// <param name="workflowJson">工艺流程JSON字符串</param>
        /// <returns>排序后的工艺流程</returns>
        [HttpPost("SortProcessFlowJson")] 
        public List<StepNode> SortProcessFlowJson([FromBody] string workflowJson)
        {
            try
            {
                var workflow = JsonConvert.DeserializeObject<WorkflowSortInput>(workflowJson, new JsonSerializerSettings 
                { 
                    StringEscapeHandling = StringEscapeHandling.Default
                });
                
                if (workflow == null)
                {
                   
                }
                
                return _processModelAppService.SortProcessFlow22(workflow);
            }
            catch (System.Text.Json.JsonException ex)
            {
                return new List<StepNode>();
            }
            catch (Exception ex)
            {
                return new List<StepNode>();
            }
        }
    }
} 